//
//  LS_StatusRetweetedView.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/25.
//  Copyright © 2016年 Apple. All rights reserved.
//转发微博的视图

import UIKit
import SnapKit
class LS_StatusRetweetedView: UIView {
    //记录转发微博的底部约束
    var bottomCons:Constraint?
    //绑定数据模型
    var statusViewModel: LS_StatueViewModel?{
        
        didSet{
            //给子控件绑定数据模型
            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            //给配图视图绑定模型
            //pictureView.pictures = statusViewModel?.status?.retweeted_status?.pic_urls
            //在更新约束之前需要卸载之前记录的约束
            bottomCons?.uninstall()
            //在转发微博中需要判断配图视图数组
            if let urls = statusViewModel?.status?.retweeted_status?.pic_urls where urls.count > 0 {
                //有配图的转发微博
                //设置数据
                pictureView.pictures = urls
                //修稿底部约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                    
                })
                //显示配图视图
                pictureView.hidden = false
            }else{
                //没有配图的转发微博
                //设置数据
                pictureView.pictures = nil
                //修改约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //隐藏配图视图
                pictureView.hidden = true
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置界面
    private func setupUI(){
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(contentLabel)
        addSubview(pictureView)
        //设置约束
        contentLabel.snp_makeConstraints { (make) in
            make.top.left.equalTo(self).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(statueCellMargin)
            make.left.equalTo(contentLabel.snp_left)
        }
        
        //给转发微博视图添加底部约束
        self.snp_makeConstraints { (make) in
           self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    
    }

      //懒加载子控件
    //显示内容的Lebel
    private lazy var contentLabel: UILabel = UILabel(title: "papapapa", font: 4, textColor: UIColor.darkGrayColor(), alignment: .Left)
    //显示配图的视图
    private lazy var pictureView: LS_StatusPictureView = LS_StatusPictureView()
    
}
